#!/bin/bash
#pMusic - external menu
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
SF='space-expand="false" space-fill="false"'
ST='space-expand="true" space-fill="true"'

template(){
	echo '
	<eventbox above-child="true" visible-window="true">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$ST'>
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="enter-notify-event">hide:'$2'</action>
	 <action signal="enter-notify-event">show:'$2'B</action>
	 <variable>'$2'</variable>
	</eventbox>
	<eventbox name="rightclick_menu" above-child="true" visible-window="true" visible="false">
	 <hbox spacing="7" border-width="3">
	  <pixmap '$SF'><height>18</height><input file>'$APPDIR'/themes/'$THEME'/'$3'</input></pixmap>
	  <text xalign="0" '$ST'>
	   <label>'$1'</label>
	  </text>
	 </hbox>
	 <action signal="leave-notify-event">show:'$2'</action>
	 <action signal="leave-notify-event">hide:'$2'B</action>
	 <action signal="button-release-event">EXIT:'$2'</action>
	 <variable>'$2'B</variable>
	</eventbox>
	'
}

export gtkdialog_menu='
<window decorated="false" border-width="0" skip_taskbar_hint="true" resizable="false" modal="true" '$ST'>
 <eventbox name="rightclick_menu_frame" above-child="false">
  <vbox border-width="1">
   <eventbox>
    <vbox spacing="0" border-width="7">
     '"
     $(template " $(gettext 'Browse downloads (in main window)')" browse icon_directory.svg)
     $(template " $(gettext 'Add download directory to Favorites')" add_favorites icon_favorites_add.svg)
     <hseparator></hseparator>
     $(template " $(gettext 'Delete preset')" delete_preset icon_delete.svg)
     "'
    </vbox>
   </eventbox>
  </vbox>
 </eventbox>
 <action signal="focus-out-event">EXIT:exit</action>
</window>'

I=$IFS; IFS=""
for STATEMENTS in $($GTKDIALOG -p gtkdialog_menu -G +"$(($PTR_X_ROOT-5))"+"$(($PTR_Y_ROOT-5))"); do
	eval $STATEMENTS
done
IFS=$I

read PRESS_EVENT < $WORKDIR/RADIOHITS_PRESS_EVENT
IFS='|' read NAME TARGET_DIR ADR C < "$STORAGE_DIR/radio-hits preset/$PRESS_EVENT"

case "$EXIT" in
	browse)
		PTR_BTN=1 #else browse will exit
		SOURCE="$TARGET_DIR"
		. $APPDIR/func -browse
		echo true > $WORKDIR/UPDATE_SOURCELIST
		;;
	add_favorites)
		gettext 'Add to Favorites' > $WORKDIR/splashtext
		$APPDIR/box_splash &
		$APPDIR/func_favorites -add_favorites "$TARGET_DIR"
		#---
		sleep 0.5
		echo 100 > $WORKDIR/splash
		;;
	delete_preset)
		rm "$STORAGE_DIR/radio-hits preset/$PRESS_EVENT"
		#kill process
		PS="$(ps -eo pid,command)"
		for I in `grep -F "$ADR" <<< "$PS" | grep -F streamripper | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
		#update list
		ls -1 "$STORAGE_DIR/radio-hits preset" | sort > $WORKDIR/radiohits_presets
		cp $WORKDIR/radiohits_presets $WORKDIR/radiohits_output
		;;
esac
